<template>
  <div class="machineMaintain">
    <DFPageList ref="dfPageListRef" :pageOptions="pageOptions" />
  </div>
</template>

<script lang="ts" setup name="machineMaintain">
  import { useMixins } from '@/hooks/useMixins';
  import {
    pageMachineMaintain,
    delMachineMaintain,
    exportMachineMaintain,
  } from '@/api/wip/machineMaintain';
  import { listMachine } from '@/api/base/machine';
  import { getDateStr, formatToDate } from '@/utils/dateUtil';

  import lang from './lang';

  const { loadLangFn, getDictDataListFn } = useMixins();

  const machineList = ref([]);

  // function disablePreviousDate(startDate, endDate) {
  //   const currentDate = new Date();
  //   // 如果结束日期晚于当前时间，则禁用
  //   if (endDate && endDate > currentDate) return true;

  //   // 如果开始日期晚于当前时间，则禁用
  //   if (startDate && startDate > currentDate) return true;

  //   return false; // 其他情况下不禁用
  // }

  const plannedMaintenanceDate = ref({
    startTime: `${getDateStr(-30)} 00:00:00`,
    endTime: `${getDateStr(30)} 23:59:59`,
    defaultValue: [
      new Date(`${getDateStr(-30)} 00:00:00`).getTime(),
      new Date(`${getDateStr(30)} 23:59:59`).getTime(),
    ],
  });

  const machineTypeList = ref([]);
  const maintenanceTypeList = ref([]);
  const maintainedList = ref([]);
  const maintenanceStatusList = ref([]);

  const beforeResetFn = () => {};

  const afterResetFn = () => {};

  loadLangFn(lang);

  const pageOptions = {
    searchForm: {
      formSchemas: [
        // 保养单号
        {
          label: 'machineMaintainCode',
          component: 'NInput',
        },
        // 保养名称
        {
          label: 'machineMaintainName',
          component: 'NInput',
        },
        // 设备名称
        {
          label: 'machineName',
          prop: 'machineCode',
          component: 'NSelect',
          options: machineList,
        },
        // 保养人
        {
          label: 'maintenanceUser',
          component: 'NInput',
        },
        // 计划保养日期
        {
          label: 'planMaintenanceDate',
          component: 'NDatePicker',
          defaultValue: plannedMaintenanceDate.value.defaultValue,
          componentProps: {
            clearable: true,
            type: 'daterange',
            valueFormat: 'yyyy-MM-dd',
            format: 'yyyy-MM-dd',
            defaultTime: ['00:00:00', '23:59:59'],
            // isDateDisabled: disablePreviousDate,
            // isTimeDisabled:(time: number) => {
            //   return getTimeDisabled(time,1);
            // },
            onUpdateValue: (_, v: string[]) => {
              if (v) {
                plannedMaintenanceDate.value.startTime = v[0];
                plannedMaintenanceDate.value.endTime = v[1];
              } else {
                plannedMaintenanceDate.value.startTime = '';
                plannedMaintenanceDate.value.endTime = '';
              }
            },
          },
        },
        // 保养类型
        {
          label: 'periodRateName',
          prop: 'periodRateCodeList',
          component: 'NSelect',
          options: maintenanceTypeList,
          componentProps: {
            multiple: true,
          },
        },
        // 是否已保养
        {
          label: 'isMaintenance',
          component: 'NSelect',
          options: maintainedList,
        },
        // 保养状态
        {
          label: 'maintenanceStatus',
          component: 'NSelect',
          options: maintenanceStatusList,
        },
      ],
      beforeResetFn,
      afterResetFn,
    },
    table: {
      hasSelection: true,
      // importTemplateType: '10',
      // importUrl: '/crm/crmTblTestStandards/importExcel',
      // rowKey: 'name',
      columns: [
        {
          label: 'machineMaintainCode',
        },
        {
          label: 'machineMaintainName',
        },
        {
          label: 'machineCode',
        },
        {
          label: 'machineName',
        },
        {
          label: 'periodRateName',
        },
        {
          label: 'machineModel',
        },
        // {
        //   label: 'machineType',
        // },
        {
          label: 'planMaintenanceDate',
          render: (row) => {
            return h('span', {
              innerText: row.planMaintenanceDate
                ? formatToDate(new Date(row.planMaintenanceDate), 'yyyy-MM-dd')
                : '-',
            });
          },
        },
        {
          label: 'maintenanceUser',
        },
        {
          label: 'maintenanceStatus',
        },
        {
          label: 'maintenanceDate',
          render: (row) => {
            return h('span', {
              innerText: row.maintenanceDate
                ? formatToDate(new Date(row.maintenanceDate), 'yyyy-MM-dd')
                : '-',
            });
          },
        },
        {
          label: 'isMaintenance',
        },
      ],
      action: {
        columns: [
          {
            label: 'check',
            hump: true,
          },
          {
            label: 'edit',
            hump: true,
          },
          {
            label: 'delete',
          },
        ],
      },
      operaColumn: [
        {
          label: 'add',
          hump: true,
        },
        // {
        //   label: 'delete',
        // },
        // {
        //   label: 'import',
        // },
        {
          label: 'export',
        },
      ],
      requestFn: pageMachineMaintain,
      delRequestFn: delMachineMaintain,
      exportRequestFn: exportMachineMaintain,
    },
  };

  // 发送请求获取设备名称数据
  const initFn = async () => {
    getMachineNameFn();
    getMachineTypeListDataFn();
  };
  // 获取设备名称
  const getMachineNameFn = async () => {
    const res = await Promise.all([listMachine()]);
    const [list1] = res;

    machineList.value =
      list1?.data.map((item) => ({
        label: item.machineName + `(${item.machineCode})`,
        value: item.machineCode,
      })) || [];
  };

  async function getMachineTypeListDataFn() {
    const res = await Promise.all([
      getDictDataListFn('machine_type'),
      getDictDataListFn('maintenance_period_type'),
      getDictDataListFn('is_maintain'),
      getDictDataListFn('maintenance_status'),
    ]);

    const [list1, list2, list3, list4] = res;

    machineTypeList.value = list1;
    maintenanceTypeList.value = list2;
    maintainedList.value = list3;
    maintenanceStatusList.value = list4;
  }
  initFn();
</script>
